package com.ys.controller.web.business;


import com.bo.vo.W;
import com.ys.config.MenuId;
import com.ys.config.MyConfig;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.business.TpProductBq;
import com.ys.entity.business.TpProductBqSp;
import com.ys.entity.business.TpProductBqSpZj;
import com.ys.entity.business.TpProductMaterial;
import com.ys.entity.sys.YsProcInst;
import com.ys.enume.business.TpProductMaterialEnum;
import com.ys.enume.flow.YsProcInstEnum;
import com.ys.service.sys.CommonApiService;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/web/business/tpProductBqSp")
public class WebTpProductBqSpController {

    private static final Logger logger = LoggerFactory.getLogger(WebTpProductBqSpController.class);

    @Autowired
    private CommonApiService<TpProductBqSp> commonApiService;


    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,
                         Long id) {
        try {
            if (id == null){
                return com.ys.vo.W.error(model, "id不能为空");
            }
            TpProductBqSp entity = new TpProductBqSp().setId(id).queryFirst();

            Map<String, Object> extraParam = new TpProductBqSp().setId(Long.valueOf(id))
                    .addLeftJoin("TB_BaseinfoSppl D ON  D.id = A.CommodityPlId")
                    .addLeftJoin("TB_BaseinfoDwmc G ON  G.id = A.NameUnitId")
                    .addLeftJoin("TB_BaseinfoSppm W ON  W.id = A.NameId")
                    .addLeftJoin("TB_BaseinfoSpyt H ON  H.id = A.UsageId")
                    .addLeftJoin("TB_BaseinfoLmwh O ON  O.id = A.CommodityLmId")
                    .addLeftJoin("TB_BaseinfoLmwh Q ON  Q.id = A.CateOneJPId")
                    .addField("D.categoryCn AS commodityPl")
                    .addField("G.NameUnitCn AS nameUnit")
                    .addField("W.NameCn AS nameCn,W.NameEn AS nameEn ")
                    .addField("H.UsageCn AS usageCn,H.UsageEn AS usageEn ")
                    .addField("O.CategoryPath AS vspLm1")
                    .addField("Q.CategoryPath AS cateOneJp")
                    .queryFirstMap();
            List<TpProductMaterial> tpProductMaterialList = new TpProductMaterial().setType(TpProductMaterialEnum.Type.TP_ProductBqSp.value).where("A.ProductCode = '" + entity.getCode() + "'").queryList();

            if (tpProductMaterialList.size() != 0){
                // 处理商品材质列表
                StringBuilder materialCnList = new StringBuilder();
                StringBuilder materialEnList = new StringBuilder();
                StringBuilder materialIdList = new StringBuilder();
                for (int i = 0; i < tpProductMaterialList.size(); i++) {
                    materialCnList.append(","+tpProductMaterialList.get(i).getMaterialNameCn());
                    materialEnList.append(","+tpProductMaterialList.get(i).getMaterialNameEn());
                    materialIdList.append(","+tpProductMaterialList.get(i).getId());
                }
                extraParam.put("materialCn",materialCnList.substring(1,materialCnList.length()));
                extraParam.put("materialEn",materialEnList.substring(1,materialEnList.length()));
                extraParam.put("materialId",materialIdList.substring(1,materialIdList.length()));
            }

            TpProductBq tpProductBq = new TpProductBq().setId(entity.getPid()).queryFirst();

            List<Map<String, Object>> tpProductBqSqZjList = new TpProductBqSpZj()
                    .setPid(Long.valueOf(id))
                    .queryMap();

            YsProcInst yspProcInst = new YsProcInst().setBid(tpProductBq.getBid()).queryFirst();
            Integer onProcess = yspProcInst.getStateFlow() == YsProcInstEnum.StateFlow.INIT.value ? 0 : 1;
            if (!"test".equals(MyConfig.getActive()) && !"pro".equals(MyConfig.getActive())) {
                SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TP_ProductBqSp, tpProductBqSqZjList);
                return commonApiService.toEdit(model, id, new TpProductBqSp(), MenuId.TP_ProductBqSp, extraParam, subGridConfig);
            }else {
                commonApiService.toEdit(model, id, new TpProductBqSp(), MenuId.TP_ProductBqSp, extraParam, null);
                model.addAttribute("sonlist", tpProductBqSqZjList);
                model.addAttribute("onProcess", onProcess);
                return "business/tpProductBq/tpProductBqSpEdit";
            }
        }catch (Exception e) {
            logger.error("商品备案申请详情编辑页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }

    @Data
    @AllArgsConstructor
    private static class TpProductBqSpEdit{
        private Long id;
    }
}
